<?php
declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace Plugin\Organization\Http\Controller\Api;

use App\Http\Admin\Controller\AbstractController as AdminAbstractController;
use App\Http\Common\Middleware\AccessTokenMiddleware;
use App\Http\Common\Result;
use App\Http\CurrentUser;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\Swagger\Annotation\Get;
use Hyperf\Swagger\Annotation\HyperfServer;
use Hyperf\Swagger\Annotation\JsonContent;
use Hyperf\Swagger\Annotation\Post;
use Hyperf\Swagger\Annotation\RequestBody;
use Hyperf\Validation\Annotation\Scene;
use Mine\Swagger\Attributes\PageResponse;
use Hyperf\Swagger\Annotation\Put;
use Plugin\Organization\Http\Request\Admin\RdOrganizeUserRequest;
use Plugin\Organization\Service\RdOrganizeUserService;

/**
 * 工区账号公司关联表控制器
 * Class RdOrganizeUserApiController
 */
#[HyperfServer(name: 'http')]
final class RdOrganizeUserApiController extends AdminAbstractController
{

     /**
     * 业务处理服务
     * RdOrganizeUserService
     */
    public function __construct(
        protected readonly RdOrganizeUserService $service,
        protected readonly CurrentUser $currentUser
    ) {}
    #[Post(
        path: '/organization/rdOrganizeUser/api/login',
        summary: '微信登录',
        tags: ['工区账号公司关联表控制器'],
    )]
    #[RequestBody(
        content: new JsonContent(ref: RdOrganizeUserRequest::class, title: '创建工区账号公司关联表控制器')
    )]
    #[PageResponse(instance: new Result())]
    #[Scene('login')]
    public function login(RdOrganizeUserRequest $request): Result
    {
        if ($request->all()['type'] == 1) {
            return $this->success($this->service->loginByPhone($request->all()));
        }
        return $this->success($this->service->loginByWx($request->all()));
    }

    #[Post(
        path: '/organization/rdOrganizeUser/api/bind',
        summary: '绑定手机号',
        tags: ['工区账号公司关联表控制器'],
    )]
    #[RequestBody(
        content: new JsonContent(ref: RdOrganizeUserRequest::class, title: '绑定工区账号公司关联表控制器')
    )]
    #[PageResponse(instance: new Result())]
    #[Scene('bind')]
    public function bind(RdOrganizeUserRequest $request): Result
    {
        return $this->success($this->service->bindPhone($request->all()));
    }

    #[Post(
        path: '/organization/rdOrganizeUser/api/vcode',
        summary: '获取验证码',
        tags: ['工区账号公司关联表控制器'],
    )]
    #[RequestBody(
        content: new JsonContent(ref: RdOrganizeUserRequest::class, title: '创建工区账号公司关联表控制器')
    )]
    #[PageResponse(instance: new Result())]
    #[Scene('vcode')]
    public function vcode(RdOrganizeUserRequest $request): Result
    {
        return $this->success($this->service->vCode($request->all()));
    }

    #[Get(
        path: '/organization/rdOrganizeUser/api/user',
        summary: '获取列表',
        tags: ['工区账号公司关联表控制器'],
    )]
    #[PageResponse(instance: new Result())]
    #[Middleware(middleware: AccessTokenMiddleware::class, priority: 100)]
    public function user(): Result
    {
        return $this->success($this->service->getUser($this->currentUser->id()));
    }

    #[Put(
        path: '/organization/rdOrganizeUser/api/update',
        summary: '修改工区账号公司关联表控制器',
        tags: ['工区账号公司关联表控制器'],
    )]
    #[RequestBody(
        content: new JsonContent(ref: RdOrganizeUserRequest::class, title: '修改工区账号公司关联表控制器')
    )]
    #[PageResponse(instance: new Result())]
    #[Middleware(middleware: AccessTokenMiddleware::class, priority: 100)]
    public function update(RdOrganizeUserRequest $request): Result
    {
        return $this->success($this->service->update($this->currentUser->id(), $request->all()));
    }


}